package hn.cch.init;

import hn.cch.cache.PipeCache;
import hn.cch.constant.Path;
import hn.cch.model.Pipe;
import hn.cch.util.StreamUtil;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

public class InitPipe {
    private static Logger logger = LoggerFactory.getLogger(InitPipe.class);

    /**
     * 读取YAML格式配置文件初始化通道信息
     */
    public static void initPipe(File file){
        Yaml yaml = new Yaml();
        InputStream inputStream = StreamUtil.toInput(file);
        ArrayList<LinkedHashMap<String, String>> arrayList;
        arrayList = (ArrayList<LinkedHashMap<String, String>>) yaml.load(inputStream);
        logger.info("pipe size : " + arrayList.size());
        for (LinkedHashMap<String, String> linkedHashMap:arrayList) {
            Pipe pipe = new Pipe();
            pipe.setPipeId(linkedHashMap.get("pipeId"));
            pipe.setHostIp(linkedHashMap.get("hostIp"));
            pipe.setUserId(linkedHashMap.get("userId"));
            PipeCache.setKeyValue(pipe.getPipeId(), pipe);
            logger.info("pipe cache : " + pipe.getPipeId());
        }

    }

    public static void main(String[] args) {
        initPipe(new File(Path.PIPE_PATH));
    }
}
